﻿using System;
using System.Collections.Generic;
using System.Linq;
using Common.Extension;
using Common.Model;
using Common.util;
using Dapper;
using log4net;

namespace Common.Dao
{
    public class NewBlockDao
    {
        private static readonly ILog Log = LogManager.GetLogger(typeof(NewBlockDao));
        
        //保存新的板块
        public static void BatchSaveNewBlocks(List<NewBlock> blocks)
        {
            String sql =
                @"INSERT INTO `new_block`(block_name,`from`,manual_handle_status,event_count,create_time) " +
                "VALUES (@BlockName,@From,@ManualHandleStatus,@EventCount,@CreateTime) ON DUPLICATE KEY update event_count = @EventCount";
            using (var conn = DbUtils.GetHotTopicConnection())
            {
                var execute = conn.InsertMultiple(sql, blocks);
                Log.Info("insert count = " + execute);
            }
        }
        
        //获取全部的新版块
        public static List<NewBlock> GetAllNewBlocks()
        {
            String sql =
                "SELECT block_name AS BlockName,`from`,manual_handle_status AS ManualHandleStatus,create_time,update_time AS updateTime AS CreateTime,event_count AS EventCount FROM `new_block`";

            using (var conn = DbUtils.GetHotTopicConnection())
            {
                List<NewBlock> list = conn.Query<NewBlock>(sql)?.ToList();
                Log.Info("query record count = " + list?.Count);
                if (list != null && list.Any())
                {
                    return list.ToList();
                }
                else
                {
                    return null;
                }
            }
        }
        
        //获取全部的新版块
        public static List<NewBlock> GetNewBlocksDroped()
        {
            String sql =
                "SELECT block_name AS BlockName,`from`,manual_handle_status AS ManualHandleStatus,create_time AS CreateTime,update_time AS updateTime,event_count AS EventCount FROM `new_block` " +
                " WHERE manual_handle_status != 2";

            using (var conn = DbUtils.GetHotTopicConnection())
            {
                List<NewBlock> list = conn.Query<NewBlock>(sql)?.ToList();
                Log.Info("query record count = " + list?.Count);
                if (list != null && list.Any())
                {
                    return list.ToList();
                }
                else
                {
                    return null;
                }
            }
        }
    }
}